﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace PictureBoard4.Shape
{
   public class DrawTool
    {
       //绘制白色底片
       public static void BackColor(Shape Shape, Graphics g) 
       {
           //坐标
           Point StartPoint = new Point();
           Point EndPoint = new Point();
           if(Shape.GetBound(ref StartPoint,ref EndPoint))
           {
               Pen p = new Pen(Color.Blue, 2);
               //绘制虚线
               p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
               if (StartPoint.X > EndPoint.X)
               {
                   g.FillRectangle(new SolidBrush(Color.White), new Rectangle(EndPoint.X, EndPoint.Y, Math.Abs(StartPoint.X - EndPoint.X), Math.Abs(StartPoint.Y - EndPoint.Y)));
               }
               else 
               {
                   g.FillRectangle(new SolidBrush(Color.White), new Rectangle(StartPoint.X, StartPoint.Y, Math.Abs(StartPoint.X - EndPoint.X), Math.Abs(StartPoint.Y - EndPoint.Y)));
               }
           }
       }
       //绘制边框
       public static void DrawBoard(Shape shape, Graphics g) 
       {
           //坐标
           Point StartPoint = new Point();
           Point EndPoint = new Point();
           //矩形边框
           if (shape.GetBound(ref StartPoint, ref EndPoint))
           {
               Pen p = new Pen(Color.Blue, 2);
               p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
               int realStartX = Math.Min(StartPoint.X, EndPoint.X);
               int realStartY = Math.Min(StartPoint.Y, EndPoint.Y);
               int realEndX = Math.Max(StartPoint.X, EndPoint.X);
               int realEndY = Math.Max(StartPoint.Y, EndPoint.Y);
               int minStartX = StartPoint.X, minStartY = StartPoint.Y, maxEndX = 0, maxEndY = 0;
               minStartX = Math.Min(minStartX, realStartX);
               minStartY = Math.Min(minStartY, realStartY);
               maxEndX = Math.Max(maxEndX, realEndX);
               maxEndY = Math.Max(maxEndY, realEndY);
               //以下是为了获取最大重绘矩形。
               Rectangle refeshRect = new Rectangle(minStartX, minStartY, maxEndX - minStartX, maxEndY - minStartY);
               refeshRect.Inflate(1, 1);//重绘矩形的大小扩展1个单位
               g.DrawRectangle(p, refeshRect);
           }
       }
       //得到边界
       public static bool IsBound(Shape Shape, Point TempPoint)
       {
           //得到坐标
           Point StartPoint = new Point();
           Point EndPoint = new Point();

           if (Shape.GetBound(ref StartPoint, ref EndPoint))
           {
               if (Math.Abs(StartPoint.X - EndPoint.X)
                   == Math.Abs(TempPoint.X - StartPoint.X)
                   + Math.Abs(TempPoint.X - EndPoint.X))
               {
                   if (Math.Abs(StartPoint.Y - EndPoint.Y)
                   == Math.Abs(TempPoint.Y - StartPoint.Y)
                   + Math.Abs(TempPoint.Y - EndPoint.Y))
                   {
                       return true;
                   }
               }
               return false;
           }
           return false;
       }
    }
}
